<?php

class GroupController extends Zend_Controller_Action
{

    public function init()
    {
        try{
    		$session = Zend_Registry::get('session');
    		
    		if($session->user['Username'] != ""){ //ako ne e prazna sesijata znaci deka korisnikot e logiran.
    			
    		}        
        	else{        		
    			$this->_helper->redirector('index','login'); //vo sprotivno vrati go na stranata za logiranje.
    		} 
        
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        } 
    }

    public function indexAction()
    {
    	try{ 
        	$session = Zend_Registry::get('session');	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n"; }
    	
		$groupOB = new Application_Model_DbTable_Groups();
		$groupmemOB = new Application_Model_DbTable_Groupmembership();
		$userOB = new Application_Model_DbTable_Users();
		
		$groupsOfUser = $groupmemOB->getAllGroupsForUser($session->user['id']); //site stavki od Groupmembership tabelata vo koi go ima ID-to na logiraniot user
		
		$this->view->gm = $groupsOfUser;
		$this->view->groupOB = $groupOB;
		$this->view->userOB = $userOB;
		$this->view->userID = $session->user['id'];
		/*$groupArray = array();  // kreiraj prazna niza
		
		foreach ($groupsOfUser as $group)
		{
			$groupArray = $groupOB->getGroup($group['Group_id']);
		}	//popolni ja so grupi vo koi clenuva korisnikot
		
		$this->view->groups = $groupArray; */
    }

    public function createnewAction()
    {
        try{    
    		$session = Zend_Registry::get('session');
    		$groups_BO = new Application_Model_DbTable_Groups();
    		$groupMembership_BO = new Application_Model_DbTable_Groupmembership();
    		$user_id = $session->user['id'];
    		
    		if ($this->getRequest()->isPost()) {
	    				if (isset($_POST['btn_CreateGroup'])){
	    					$groupName = $this->getRequest()->getParam("txt_GroupName");
	    					$groupInfo = $this->getRequest()->getParam("txt_Info");
	    					$groupDescription = $this->getRequest()->getParam("txt_Description");
	    					
	    					$last=$groups_BO->AddGroup($groupName,$groupDescription,$groupInfo,$session->user['id'] );	    					
	    					$groupMembership_BO->AddGroupMembership(1, $user_id, $last);
	    					$this->_helper->redirector('index','group');
	    				}
    		}
    		
    		
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }

    public function viewgroupAction()
    {
        try{
        	$session = Zend_Registry::get('session');
    		$groups_BO = new Application_Model_DbTable_Groups();
    		$groupPosts_BO = new Application_Model_DbTable_Groupposts();
    		$groupMembership_BO = new Application_Model_DbTable_Groupmembership();
    		$user_id = $session->user['id'];    		
    		
        	if ($this->_request->getQuery('id') != ""){
        		$group_id = $this->_request->getQuery('id');  		
	    		if ($this->getRequest()->isPost()) {
	    				if (isset($_POST['btn_GroupPostSubmit'])){
	    					$message =  $this->getRequest()->getParam("txt_GroupPost");
	    					//$user_id = $session->user['id'];	    					
	    					$time = new Zend_Date();
							$time=$time->toString('YYYY-MM-dd HH:mm:ss');	    				
	    												
							//add post to group
							$groupPosts_BO->addGroupPost($message, $time, $user_id, $group_id);							
	    				}
	    				elseif (isset($_POST['btn_GroupJoin'])){
	    					//$user_id = $session->user['id'];
	    					$membershipType =0;
	    					$group_id=$this->_request->getQuery('id');
	    					$groupMembership_BO->AddGroupMembership($membershipType, $user_id, $group_id);	    				
	    				}
	    				elseif (isset($_POST['btn_GroupLeave'])){
							//$user_id = $session->user['id'];
	    					$groupMembership_BO->deleteGroupMemberShip($user_id,$group_id);	    					
	    				}
	    				
	    				
	    		}
	    		$posts =$groups_BO->getGroupPosts($group_id);
				$group = $groups_BO->getGroup($group_id);
				$canview = $groupMembership_BO->getGroupMembershipUser($user_id,$group_id);
				//echo $canview;
				
				if ($canview==null){
					$flag_view = 0;
				}
				else{
					$flag_view = 1;	
				}
							
							
	    		if ($group == null) {
	    			$flag_group = 0;
	    		}
	    		else{
	    			$flag_group = 1;
	    		}
	    		$this->view->posts = $posts;
	    		$this->view->group = $group;
	    		$this->view->flag_group = $flag_group;
	    		$this->view->flag_view = $flag_view;
	    		
	    	}
	    	else{
	    		$this->_helper->redirector('index','group');
	    	}
    		
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    }

    public function leavegrpAction()
    {
    	$group_id = $this->getRequest()->getParam('grpID');
    	$user_id = $this->getRequest()->getParam('userID');
    	
        $groupMembership_BO = new Application_Model_DbTable_Groupmembership();
    	$groupMembership_BO->deleteGroupMemberShip($user_id, $group_id);
    	
    	$this->_helper->redirector('index');
    }


}







